package org.vnetcon.blobdb.database.ws;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.vnetcon.blobdb.database.WsAuthenticationInterface;

public class DefaultTableAuthentication implements WsAuthenticationInterface {

	static boolean driverRegistered = false;
	
	
	@Override
	public boolean allow(String username, String password, String sql) {
		String bSQLCreate = "CREATE TABLE SYSTEM.USER (150, 4000) ( USERNAME CHAR(50), PASSWORD CHAR(50) ) ;";
		String bSQLSelect = "SELECT USERNAME FROM SYSTEM.USER WHERE USERNAME == '" + username + "' && PASSWORD == '" + password + "' ;";
		boolean bRet = false;

		
		try{
			
			if(!driverRegistered){
				Class.forName("org.vnetcon.blobdb.driver.jdbc.BlobDBDriver").newInstance();
				driverRegistered = true;
			}
			

			Connection con = DriverManager.getConnection("blobdb");
			Statement stmt = con.createStatement();
			try {
				ResultSet rs = stmt.executeQuery(bSQLSelect);
				if (rs.next()) {
					bRet = true;
				}
				rs.close();
			} catch (Exception exx) {
				stmt.executeUpdate(bSQLCreate);
			}
			stmt.close();
			return bRet;
		}catch(Exception ex){
			return false;
		}
		
	}

}
